package com.swjd.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.swjd.po.user;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		// 判断是否访问登陆页面 （即判断访问链接是否包含/login.action,包含则放行）
		String uri = request.getRequestURI();
		if (uri.indexOf("/login.action") >= 0) {
			return true;
		}
		// 如果访问的不是登陆页面，则判断是否已登陆
		HttpSession session = request.getSession();
		user user = (user) session.getAttribute("USER_SESSION");
		if (user != null) {
			return true;
		}
		request.setAttribute("msg", "您还没有登陆，请先登陆！");
		request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
		return false;
	}

}
